<?php
class TPLAdminCatalog {
	protected static function Menu($act='')
	{
		$lang=Eleanor::$Language[$GLOBALS['Eleanor']->module['config']['n']];
		$links = &$GLOBALS['Eleanor']->module['links'];

		$categs=isset($GLOBALS['Eleanor']->module['navigation']['categories']) ? $GLOBALS['Eleanor']->module['navigation']['categories'] : false;
		$options=isset($GLOBALS['Eleanor']->module['navigation']['options']) ? $GLOBALS['Eleanor']->module['navigation']['options'] : false;
		$GLOBALS['Eleanor']->module['navigation']=array(
		array($links['list'],$lang['list'],'act'=>$act=='list',
		'submenu'=>array(
		array($links['add'],$lang['add'],'act'=>$act=='add'),
		),
		),
		$options ? $options : array($links['options'],Eleanor::$Language['main']['options'],'act'=>$act=='options'),
		$categs ? $categs : array($links['categories'],$lang['cats_manage']),
		//array($links['addonf'],$lang['addonf'],'act'=>$act=='addonf'),
		);
	}
	public static function CatalogList($items,$cnt,$pp,$qs,$page, $links) {
		static::Menu();
		$GLOBALS['jscripts'][]='js/checkboxes.js';
		$lang=Eleanor::$Language[$GLOBALS['Eleanor']->module['config']['n']];
		$fititlet=$statuses='';

		$qs+=array(''=>array());
		$qs['']+=array('fi'=>array());
		$fs=(bool)$qs['']['fi'];
		$qs['']['fi']+=array(
			'title'=>false,
			'titlet'=>false,
			'status'=>false,
			'category'=>false,
		);
		$temp=array(
			'b'=>$lang['begins'],
			'q'=>$lang['match'],
			'e'=>$lang['endings'],
			'm'=>$lang['contains'],
		);
		foreach($temp as $k=>&$v)
			$fititlet.=Eleanor::Option($v,$k,$qs['']['fi']['titlet']==$k);
		$temp=array(
			-1=>$lang['waitmod'],
			0=>$lang['blocked'],
			1=>$lang['active'],
		);
		foreach($temp as $k=>&$v)
			$statuses.=Eleanor::Option($v,$k,$qs['']['fi']['status']!==false and $qs['']['fi']['status']==$k);
		$c='<form method="post">
			<table class="tabstyle tabform" id="ftable">
				<tr class="infolabel"><td colspan="2"><a href="#">'.$lang['filters'].'</a></td></tr>
				<tr>
					<td><b>Title</b><br />'.Eleanor::Select('fi[titlet]',$fititlet,array('style'=>'width:30%')).Eleanor::Input('fi[title]',$qs['']['fi']['title'],array('style'=>'width:68%')).'</td>
					<td><b>'.$lang['category'].'</b><br>'.Eleanor::Select('fi[category]',Eleanor::Option('-'.$lang['nocat'].'-',0).Eleanor::Option($lang['nocat'],'no',$qs['']['fi']['category']=='no').$GLOBALS['Eleanor']->Categories->GetOptions($qs['']['fi']['category'])).'</td>
				</tr>
				<tr>
					<td><b>'.$lang['status'].'</b><br />'.Eleanor::Select('fi[status]',Eleanor::Option('-'.$lang['nocat'].'-','-').$statuses).'</td>
					<td style="text-align:center;vertical-align:middle">'.Eleanor::Button('apply').'</td>
				</tr>
			</table>
<script type="text/javascript">//<![CDATA[
$(function(){
	var fitrs=$("#ftable tr:not(.infolabel)");
	$("#ftable .infolabel a").click(function(){
		fitrs.toggle();
		$("#ftable .infolabel a").toggleClass("selected");
		return false;
	})'.($fs ? '' : '.click()').';
	One2AllCheckboxes("#checks-form","#mass-check","[name=\"mass[]\"]",true);
});//]]></script>
		</form><form id="checks-form" action="'.$GLOBALS['Eleanor']->Url->Construct($qs+array('page'=>$page)).'" method="post" onsubmit="return (CheckGroup(this) && confirm(\'are_you_sure\'))">';

		$Lst=Eleanor::LoadListTemplate('table-list',6)->begin(
			array('Title','sort'=>$qs['sort']=='title' ? $qs['so'] : false,'href'=>$GLOBALS['Eleanor']->Url->Construct(array_merge($qs,array('sort'=>'title','so'=>$qs['sort']=='title' && $qs['so']=='asc' ? 'desc' : 'asc')))),
			$lang['category'],
			array($lang['date'],'sort'=>$qs['sort']=='date' ? $qs['so'] : false,'href'=>$GLOBALS['Eleanor']->Url->Construct(array_merge($qs,array('sort'=>'date','so'=>$qs['sort']=='date' && $qs['so']=='asc' ? 'desc' : 'asc')))),
			array($lang['author'],'sort'=>$qs['sort']=='author' ? $qs['so'] : false,'href'=>$GLOBALS['Eleanor']->Url->Construct(array_merge($qs,array('sort'=>'author','so'=>$qs['sort']=='author' && $qs['so']=='asc' ? 'desc' : 'asc')))),
			array('Filter',80,'sort'=>$qs['sort']=='id' ? $qs['so'] : false,'href'=>$GLOBALS['Eleanor']->Url->Construct(array_merge($qs,array('sort'=>'id','so'=>$qs['sort']=='id' && $qs['so']=='asc' ? 'desc' : 'asc')))),
			array(Eleanor::Check('mass',false,array('id'=>'mass-check')),20)
		);
		if($items)
		{
			$editimg=Eleanor::$Template->default['theme'].'images/' . 'edit.png';
			$delimg=Eleanor::$Template->default['theme'].'images/' . ('delete.png');
			$inimg=Eleanor::$Template->default['theme'].'images/' . ('inactive.png');
			$acimg=Eleanor::$Template->default['theme'].'images/' . ('active.png');
			$t=time();
			foreach($items as &$v)
			{
				$cats='';
				foreach($v['cats'] as &$cv)
					if(isset($GLOBALS['Eleanor']->Categories->dump[$cv]))
						$cats.=($cats ? '' : '<b>').$GLOBALS['Eleanor']->Categories->dump[$cv]['title'].($cats ? '' : '</b>').', ';
				$c.=$Lst->item(
					'<a id="it'.$v['id'].'" href="'.($edit=$GLOBALS['Eleanor']->Url->Construct(array('edit'=>$v['id']))).'">'.$v['title'].'</a>',
					$cats ? rtrim($cats,', ') : array('--','center'),
					array((int)$v['pinned'] && $v['_pinned'] ? $v['pinned'] : $v['date'],'center'),
					$v['author_id'] ? '<a href="'.Eleanor::$Login->UserLink(htmlspecialchars_decode($v['author'],ELENT),$v['author_id']).'">'.$v['author'].'</a>' : $v['author'],
					$Lst->func(
						(int)$v['enddate'] && strtotime($v['enddate'])<=$t ? '<img src="'.$inimg.'" alt="" title="'.strip_tags(['endeddate']).'" />' : array($GLOBALS['Eleanor']->Url->Construct(array('swap'=>$v['id'])),$v['status']<=0 ? 'activate' : 'deactivate',$v['status']<0 ? $wimg : ($v['status']==0 ? $inimg : $acimg)),
						array($edit,'Edit',$editimg),
						array($GLOBALS['Eleanor']->Url->Construct(array('delete'=>$v['id'])),'Delete',$delimg)
					),
					Eleanor::Check('mass[]',false,array('value'=>$v['id']))
				);
			}
		}
		else
			$Lst->empty($lang['not_found']);
		$c.=$Lst->end().'<div class="submitline" style="text-align:right"><div style="float:left">'.sprintf($lang['tto_pages'],$Lst->perpage($pp,$links['pp'])).'</div>With selected'.Eleanor::Select('op',Eleanor::Option('activate','a').Eleanor::Option('deactivate','d').Eleanor::Option('Delete','k').Eleanor::Option($lang['waitmod'],'m')).Eleanor::Button('OK').'</div></form>';
		return Eleanor::$Template->Cover($c.Eleanor::$Template->Pages($cnt,$pp,$page,$qs));
	}

	public static function AddEdit($id,$values,$error,$uploader,$bypost,$hasdraft) {
		static::Menu();
		$ltpl=Eleanor::$Language['tpl'];
		$lang=Eleanor::$Language[$GLOBALS['Eleanor']->module['config']['n']];
		if(Eleanor::$vars['multilang'])
		{
			$mchecks=$ml=array();
			foreach(Eleanor::$langs as $k=>&$v) {
				$mchecks[$k]=!$id || !empty($values['title'][$k]) || !empty($values['text'][$k])|| !empty($values['info'][$k]) || !empty($values['advertisement']) || !empty($values['uri'][$k]) || !empty($values['meta_title'][$k]) || !empty($values['meta_descr'][$k]);
				$ml['meta_title'][$k]=Eleanor::Input('meta_title['.$k.']',Eleanor::FilterLangValues($values['meta_title'],$k),array('tabindex'=>16));
				$ml['meta_descr'][$k]=Eleanor::Input('meta_descr['.$k.']',Eleanor::FilterLangValues($values['meta_descr'],$k),array('tabindex'=>17));
				$ml['title'][$k]=Eleanor::Input('title['.$k.']',$GLOBALS['Eleanor']->Editor->imgalt=Eleanor::FilterLangValues($values['title'],$k),array('tabindex'=>1,'id'=>'title-'.$k));
				$ml['text'][$k]=$GLOBALS['Eleanor']->Editor->Area('text['.$k.']',Eleanor::FilterLangValues($values['text'],$k),array('bypost'=>$bypost,'no'=>array('tabindex'=>6)));
				$ml['info'][$k]=$GLOBALS['Eleanor']->Editor->Area('info['.$k.']',Eleanor::FilterLangValues($values['info'],$k),array('bypost'=>$bypost,'no'=>array('tabindex'=>7)));
				$ml['advertisement'][$k]=$GLOBALS['Eleanor']->Editor->Area('advertisement['.$k.']',Eleanor::FilterLangValues($values['advertisement'],$k),array('bypost'=>$bypost,'no'=>array('tabindex'=>7)));
				$ml['uri'][$k]=Eleanor::Input('uri['.$k.']',Eleanor::FilterLangValues($values['uri'],$k),array('onfocus'=>'if(!$(this).val())$(this).val($(\'#title-'.$k.'\').val())','tabindex'=>10));
			}
		}
		else
			$ml=array(
				'meta_title'=>Eleanor::Input('meta_title',$values['meta_title'],array('tabindex'=>16)),
				'meta_descr'=>Eleanor::Input('meta_descr',$values['meta_descr'],array('tabindex'=>17)),
				'title'=>Eleanor::Input('title',$GLOBALS['Eleanor']->Editor->imgalt=$values['title'],array('tabindex'=>1,'id'=>'title')),
				'text'=>$GLOBALS['Eleanor']->Editor->Area('text',$values['text'],array('bypost'=>$bypost,'no'=>array('tabindex'=>6))),
				'info'=>$GLOBALS['Eleanor']->Editor->Area('info',$values['info'],array('bypost'=>$bypost,'no'=>array('tabindex'=>6))),
				'advertisement'=>$GLOBALS['Eleanor']->Editor->Area('advertisement',$values['advertisement'],array('bypost'=>$bypost,'no'=>array('tabindex'=>6))),
				'uri'=>Eleanor::Input('uri',$values['uri'],array('onfocus'=>'if(!$(this).val())$(this).val($(\'#title\').val())','tabindex'=>10)),
			);

		$Lst=Eleanor::LoadListTemplate('table-form');
		$c=$Lst->form()
			->begin()
			->item('Title',Eleanor::$Template->LangEdit($ml['title'],false))
			.($GLOBALS['Eleanor']->Categories->dump
				? $Lst->item($lang['categs'],Eleanor::Items('cats',$GLOBALS['Eleanor']->Categories->GetOptions($values['cats']),array('id'=>'cs','tabindex'=>2)))
					->item($lang['maincat'],Eleanor::Select('_maincat',$GLOBALS['Eleanor']->Categories->GetOptions($values['_maincat']),array('id'=>'mc','tabindex'=>3)))
				: ''
			)
			.(Eleanor::$vars['multilang'] ? $Lst->item($ltpl['set_for_langs'],Eleanor::$Template->LangChecks($values['_onelang'],$mchecks,null,4)) : '')
			.$Lst->end();

		$text=(string)$Lst->begin()
			->item(array($lang['short_text'],Eleanor::$Template->LangEdit($ml['text'],false)))
			->item(array($lang['info'],Eleanor::$Template->LangEdit($ml['info'],false)))
			->item(array($lang['advertisement'],Eleanor::$Template->LangEdit($ml['advertisement'],false)))
			->item(array($lang['price'],Eleanor::Input('price', $values['price'])))
			->item($lang['status'],Eleanor::Select('status',Eleanor::Option($lang['waitmod'],-1,$values['status']==-1).Eleanor::Option($lang['blocked'],0,$values['status']==0).Eleanor::Option($lang['active'],1,$values['status']==1),array('tabindex'=>9)))
			->end();

		$extra=$Lst->begin()
			->item('uri',Eleanor::$Template->LangEdit($ml['uri'],false))
			->item($lang['author'],Eleanor::$Template->Author($values['author'],$values['author_id'],11))
			->item(array($lang['pdate'],Dates::Calendar('date',$values['date'],true,array('tabindex'=>12)),'tip'=>$lang['pdate_']))
			->item($lang['pinned'],Dates::Calendar('pinned',$values['pinned'],true,array('tabindex'=>13)))
			->item(array($lang['enddate'],Dates::Calendar('enddate',$values['enddate'],true,array('tabindex'=>14)),'tip'=>$lang['enddate_']))
			->item($lang['reads'],Eleanor::Input('reads',$values['reads'],array('tabindex'=>15)))
			->item('Window title',Eleanor::$Template->LangEdit($ml['meta_title'],false))
			->item('Meta description',Eleanor::$Template->LangEdit($ml['meta_descr'],false))
			.($id ? $Lst->item(array($lang['ping'],Eleanor::Check('_ping',$values['_ping'],array('tabindex'=>18)),'descr'=>$lang['ping_'])) : '')
			.$Lst->end();

		$back=isset($_GET['noback']) ? '' : Eleanor::Input('back',isset($_POST['back']) ? (string)$_POST['back'] :
                getenv('HTTP_REFERER'), array('type' => 'hidden'));
		$c.=$Lst->tabs(
			array('General',$text),
			array('Settings',$extra)
		)
		->submitline((string)$uploader)
		->submitline(
			$back
			.Eleanor::Button('Ok','submit',array('tabindex'=>20))
			.($id ? ' '.Eleanor::Button('Delete','button',array('onclick'=>'window.location=\''.$GLOBALS['Eleanor']->Url->Construct(array('delete'=>$id,'noback'=>1)).'\'')) : '')
			.Eleanor::Input('_draft','n'. $id, array('type' => 'hidden'))
			// .Eleanor::$Template->DraftButton(array('do'=>'draft'),1)
			.($hasdraft ? ' <a href="'.$GLOBALS['Eleanor']->Url->Construct(array('do'=>$id ? false : 'add','edit'=>$id ? $id : false,'nodraft'=>1)).'">No draft</a>' : '')
		)
		->endform();
		return Eleanor::$Template->Cover($c,$error).'<script>//<![CDATA[
$(function(){
	$("#cs").change(function(){
		var cs=this;
		$("#mc option").each(function(i){
			if($("option:eq("+i+")",cs).prop("selected"))
				$(this).prop("disabled",false);
			else
				$(this).prop({disabled:true,selected:false});
		});
	}).change();
})//]]></script>';
	}

	public static function Categories($c) {
		static::Menu();
		return$c;
	}
}